// import Cookies from 'js-cookie'
import Cookies from 'js-cookie'

const TokenValue = 'Admin-Token'
const TokenKey = 'Token-Field'
const ConstatnsTable = 'constatns-Table'

export function getTokenKey() {
  return sessionStorage.getItem(TokenKey)
  // return Cookies.get(TokenKey)
}

export function setTokenKey(key) {
  return sessionStorage.setItem(TokenKey, key)
  // return Cookies.set(TokenKey, key)
}

export function getToken() {
  return sessionStorage.getItem(TokenValue)
  // return Cookies.get(TokenValue)
}

export function setToken(token) {
  Cookies.set(getTokenKey(), token, { expires: 1 })
  return sessionStorage.setItem(TokenValue, token)
  // return Cookies.set(TokenValue, token)
}

export function removeToken() {
  return sessionStorage.removeItem(TokenValue)
  // return Cookies.remove(TokenValue)
}

export function setConstatnsTable(errTable) {
  return localSet(ConstatnsTable, errTable)
  // return localStorage.setItem(ErrorTable, JSON.stringify(errTable))
}

export function getConstatnsTable() {
  return localGet(ConstatnsTable)
  // try {
  //   const val = localStorage.getItem(ErrorTable)
  //   return JSON.parse(val)
  // } catch (e) {
  //   return null
  // }
}

export function localSet(key, object) {
  return localStorage.setItem(key, JSON.stringify(object))
}

export function localGet(key) {
  try {
    const val = localStorage.getItem(key)
    return JSON.parse(val)
  } catch (e) {
    console.log(e)
    return null
  }
}

export const setPageParam = ($route, param) => {
  localSet('page-' + $route.path, param)
}

export const getPageQueryParams = (page, paramName) => {
  let param = page.$route.query[paramName]
  if (param && param !== '[object Object]') {
    // 保存 参数
    localSet('page-' + page.$route.path + '-' + paramName, param)
    return param
  }
  param = localGet('page-' + page.$route.path + '-' + paramName)
  if (param) {
    return param
  }
  page.$error('参数丢失')
  page.$closeTag(page.$route)

  return
}
